#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
    int count = 0;
    int ret = 0;
public:
    int kthSmallest(TreeNode* root, int k) {
        dfs(root, k);
        return ret;
    }
    void dfs(TreeNode* root, int k)
    {
        if (root == nullptr) return;

        dfs(root->left, k);
        count++;
        if (count == k)
        {
            ret = root->val;
        }
        if (count == k) return;
        dfs(root->right, k);
    }
};